<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        .item img{
            width: 240px;
            padding:10px;
        }
        .item{
            border: 1px solid #eee;
            float: left;
            font-size: 0;
        }
    </style>
</head>
<body>
    <div id="app">
        <div class="item"><img src="images/01.jpg" alt=""></div>
        <div class="item"><img src="images/02.jpg" alt=""></div>
        <div class="item"><img src="images/03.jpg" alt=""></div>
        <div class="item"><img src="images/04.jpg" alt=""></div>
        <div class="item"><img src="images/05.jpg" alt=""></div>
        <div class="item"><img src="images/06.jpg" alt=""></div>
        <div class="item"><img src="images/07.jpg" alt=""></div>
        <div class="item"><img src="images/08.jpg" alt=""></div>
        <div class="item"><img src="images/09.jpg" alt=""></div>
        <div class="item"><img src="images/10.jpg" alt=""></div>
        <div class="item"><img src="images/11.jpg" alt=""></div>
        <div class="item"><img src="images/12.jpg" alt=""></div>
    </div>
    <script>
        /* 1.得到一排放置几个元素 */
        var ww = $(window).width()
        var box = $(".item").outerWidth()
        var num = Math.floor(ww/box)
        /* 2.定义一个数组获取第一排元素的height */
        var arr = []
        $(".item").each((index,value)=>{
            if(index<num){
                // console.log(value);
                var height = $(value).outerHeight()
                arr.push(height)
            }else{
                /* 3.将元素放置在最小高度的位置，同时将数组重置 */
                var minHeight = Math.min(...arr)
                var index  = arr.indexOf(minHeight);
                var offsetLeft = $(".item").eq(index).offset().left;
                $(value).css({
                    position:"absolute",
                    left:offsetLeft,
                    top:minHeight
                })
                arr[index] = minHeight+$(value).outerHeight()
            }
        })
        console.log(arr);
    </script>
</body>
</html>